package com.koicarp.university.graudate.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.koicarp.university.graudate.common.constant.PageConstant;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

/**
 * @auther liutao
 * @Date 2020-07-30 09:44:40
 */
public class UniversityUtil {

    private static final String MOBILE_REG="0?(13|14|15|17|18)[0-9]{9}"; //国内手机
    private static final String PHONE_REG="[0-9-()（）]{7,18}"; //国内电话
    private static final String EMAIL_REG="\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
    //private static final String ID_NUMBER_REG = "\\d{17}[\\d|X]|\\d{15}";
    private static final String IP_REG = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
    /**
     * 设置响应
     *
     * @param response    HttpServletResponse
     * @param contentType content-type
     * @param status      http状态码
     * @param value       响应内容
     * @throws IOException IOException
     */
    public static void makeResponse(HttpServletResponse response, String contentType,
                                    int status, Object value) throws IOException {
        response.setContentType(contentType);
        response.setStatus(status);
        response.getOutputStream().write(JSONObject.toJSONString(value).getBytes());
    }


    /**
     * 封装前端分页表格所需数据
     *
     * @param pageInfo pageInfo
     * @return Map<String, Object>
     */
    public static Map<String, Object> getDataTable(IPage<?> pageInfo) {
        Map<String, Object> data = new HashMap<>(2);
        data.put(PageConstant.ROWS, pageInfo.getRecords());
        data.put(PageConstant.TOTAL, pageInfo.getTotal());
        return data;
    }

    /**
     * 驼峰转下划线
     *
     * @param value 待转换值
     * @return 结果
     */
    public static String camelToUnderscore(String value) {
        if (StringUtils.isBlank(value)) {
            return value;
        }
        String[] arr = StringUtils.splitByCharacterTypeCamelCase(value);
        if (arr.length == 0) {
            return value;
        }
        StringBuilder result = new StringBuilder();
        IntStream.range(0, arr.length).forEach(i -> {
            if (i != arr.length - 1) {
                result.append(arr[i]).append("_");
            } else {
                result.append(arr[i]);
            }
        });
        return StringUtils.lowerCase(result.toString());
    }

    /**
     * 判断对象是否为空
     * @param obj
     * @return
     */
    public static boolean isNotNull(Object obj){
        return obj != null && !"".equals(obj);
    }

    /**
     * 判断是否是电话号码或者手机号码
     * @param phoneNumber
     * @return
     */
    public static boolean isMoblie(String phoneNumber){
        Pattern pattern1 = Pattern.compile(MOBILE_REG);
        Pattern pattern2 = Pattern.compile(PHONE_REG);
        Matcher matcher1 = pattern1.matcher(phoneNumber);
        Matcher matcher2 = pattern2.matcher(phoneNumber);
        return matcher1.matches() || matcher2.matches();
    }

    /**
     * 校验邮箱
     * @param emailStr
     * @return
     */
    public static boolean isEmail(String emailStr){
        Pattern pattern = Pattern.compile(EMAIL_REG);
        Matcher matcher = pattern.matcher(emailStr);
        return matcher.matches();
    }

    /**
     * 校验身份证
     * @param idNumber
     * @return
     */
    public static boolean isIdNumber(String idNumber) throws Exception{
		/*Pattern pattern = Pattern.compile(ID_NUMBER_REG);
		Matcher matcher = pattern.matcher(idNumber);
		return matcher.matches();*/
        return IDCardValidateUtil.idCardValidate(idNumber);
    }

    public static boolean isIP(String ip){
        Pattern pattern = Pattern.compile(IP_REG);
        Matcher matcher = pattern.matcher(ip);
        return matcher.matches();
    }

    /**
     * 获取随机生成的UUID
     * @return
     */
    public static String getUUID(){
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-","");
        return uuid;
    }

    /**
     * 将空字符串对象转为空格字符串
     * @param str 参数
     * @return
     */
    public static String convertNull2Blank(String str){
        if (str == null || "null".equals(str) || "".equals(str)){
            return "";
        }
        return str.trim();
    }

    /**
     * 将Object数组转为String数组
     * @param arr
     * @return
     */
    public static String[] arrayClassCast(Object[] arr){
        if (arr == null){
            return null;
        }
        String[] result = new String[arr.length];
        for (int i = 0; i < arr.length;i++){
            result[i] = String.valueOf(arr[i]);
        }
        return result;
    }
    public static String nowYear(){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
       return format.format(date);
    }

    /**
     * 保留两位小数	decimal
     * @return
     */
    public static double saveTwoDecimal(double tmp){
        BigDecimal bg = new BigDecimal(tmp);
        return bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();//保留两位小数
    }

}
